package com.CGFrame.struts.bean.task;

import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.CGFrame.struts.POJOs.Confirm;
import com.CGFrame.struts.POJOs.Make;
import com.CGFrame.struts.POJOs.Reguser;
import com.CGFrame.struts.POJOs.Status;
import com.CGFrame.struts.POJOs.Task;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.bean.make.MakeBean;
import com.CGFrame.struts.bean.unit.ElementBean;
import com.CGFrame.struts.bean.unit.FilePathGenerator;
import com.CGFrame.struts.bean.unit.ShotBean;

public class TaskBean {
	Task task = null;
	Date Ve = null;
	Date Vl = null;
	
	public TaskBean(int taskId)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		this.task = (Task)pop.UniqueQuery("Task", "taskId,"+taskId);
//		calTaskTime();
		
	}
	public TaskBean(Task task)
	{
		this.task = task;
//		calTaskTime();
	}
	public String getTaskName()
	{
		return this.task.getTaskName();
	}
	
	void calTaskTime()
	{
		MakeBean makeBean = new MakeBean( task.getMake() );
		 Ve = makeBean.getVe();
		 Vl = makeBean.getVl();
	}
	public Integer getUnitId()
	{
		Make make = task.getMake();
		if (make.getShot() !=null  ) return make.getShot().getShotId();
		if (make.getElement()!=null) return make.getElement().getElementId();
		else return 0;
	}
	
	public List getUnFinishedMakes()
	{
		List preUndoneMakes = new LinkedList();
		MakeBean makeBean  =new MakeBean ( task.getMake() );
		Iterator ite_PreMakes = makeBean.getPreMakes().iterator();
		while(ite_PreMakes.hasNext())
		{
			Make make = (Make)ite_PreMakes.next();
			if ( make.getStatus().getStatusName().equals("Finished")==false && (make.getIsUsable()==null || !make.getIsUsable()) )
			{
				preUndoneMakes.add(make);
			}
		}
		
		return preUndoneMakes;
	}
	
	public Integer getProjectId()
	{
		Make make = task.getMake();
		if (make.getShot() !=null  ) return make.getShot().getShotgroup().getProject().getProjectId();
		if (make.getElement()!=null) return make.getElement().getElementgroup().getProject().getProjectId();
		else return 0;
	}
	
	public String getMakeName()
	{
		return task.getMake().getMakeName();
	}
	public boolean IsShotTask()
	{
		if ( task.getMake().getShot() != null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 *  获取文件名
	 *  参数影响文件名
	 * @param Flag  哪种文件的标志
	 * @param isExist 是新建文件，还是已存在的文件
	 * @param userName userName 提交文件人  null表示该任务制作人
	 * @return 任务相关文件名
	 */
	public StringBuffer getFileName(int Flag,String userName,boolean isExist)
	{
		if (userName==null ||userName.equals(""))
		{
			userName = this.task.getReguser().getRegName();
		}
		Make make = task.getMake();
		String makeName = make.getMakeName();
		String unitName =""; 
		String projectName ="" ;
		StringBuffer result = new StringBuffer();
		LinkedList list = null;
		if (make.getElement() != null) 
		{
//			元素不要项目和元素组开头。
			ElementBean eb=new ElementBean(make.getElement().getElementId());
//			list = eb.getElementGroupName();
			unitName = eb.getElementName();
//			projectName = eb.getProjectName();
			result.append(unitName);
		}
		else if (make.getShot() != null )
		{
			ShotBean sb=new ShotBean(make.getShot().getShotId());
			unitName=sb.getShotName();
			//项目名,镜头组名不需要，镜头名本身包含这些信息
//			list = sb.getShotGroupName();
//			projectName = sb.getProjectName();
//			result.append(projectName);
//			list.poll(); //remove Group "shots" or "elements" 
//			for(Iterator iter=list.iterator();iter.hasNext();)
//			{
//				result.append("_").append(iter.next());
//			}
			result.append(unitName);
		}
		//CCTV_001_005_003_Anim_tfeng_v01.jpg;
		// ShiTong_Anim_tfeng_v01
		
		
		
		result.append("_").append(makeName);
		try
		{
			result.append("_").append(userName);
		}
		catch(Exception e)
		{
		}
		if (Flag == FilePathGenerator.MediaFile || Flag == FilePathGenerator.MakeFile || Flag == FilePathGenerator.ChangeFile)
		{
			int versionNo = this.task.getVersionNo();
			if (isExist)
			{
				versionNo--;
			}
			if (versionNo<10 && versionNo>0)
				result.append("_v0").append(versionNo);
			else
				result.append("_v").append(versionNo);
		}
		return result;
	}
	public Date getVe()
	{
		calTaskTime();
		return Ve;
	}
	public Date getVl()
	{
		return Vl;
	}
	
	public  String getFilePathforTask(int Flag)
	{
//		path="CCTV/Project/scenes/Assets/Production/Characters/ShiTong/";   
//		path="CCTV/Project/scenes/Episodes/Episode001/Production/Shot/Shot002/";
		
		StringBuffer unitPath = new StringBuffer();
		if ( this.IsShotTask() )
		{
			unitPath =(StringBuffer)FilePathGenerator.getFilePath(this.getUnitId(),"Shot").get(0);
		}
		else
		{
			unitPath =(StringBuffer)FilePathGenerator.getFilePath(this.getUnitId(),"Element").get(0);
		}
		if (FilePathGenerator.MakeFile == Flag)
		{
			unitPath.append(this.getMakeName()).append("/Working/");
		}
		else if ( FilePathGenerator.MediaFile == Flag ||FilePathGenerator.ChangeFile == Flag)
		{
			unitPath.append("Media/Working/");
		}
		else if ( FilePathGenerator.FinishedMediaFile == Flag)
		{
			unitPath.append("Media/Approved/");
		}
		else if (FilePathGenerator.FinishedMakeFile == Flag)
		{
			unitPath.append(this.getMakeName()).append("/Approved/");
		}
		return unitPath.toString();
	}
	public Integer getVersion()
	{
		return this.task.getVersionNo();
	}
	public void updateVersion()
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		int versionNo = this.task.getVersionNo();
		task.setVersionNo(versionNo+1);
		pop.saveObject(task);
	}
	public void updateTask(String taskName,String description,int expectHours,String regUserId)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		task.setTaskName(taskName);
		task.setTaskDescription(description);
		task.setExceptHours(expectHours);
		if (regUserId != null && !(regUserId.equals("")))
		{
			Reguser r = (Reguser)pop.UniqueQuery("Reguser", "regUserId,"+regUserId);
			task.setReguser(r);
			Status status = (Status)pop.UniqueQuery("Status", "statusName,HasAssigned,statusType,Task");
			Status makeStatus = (Status)pop.UniqueQuery("Status","statusName,HasAssigned,statusType,Make");
			task.setStatus(status);
			task.getMake().setStatus(makeStatus);
			
		}
		else
		{
			Status status = (Status)pop.UniqueQuery("Status", "statusName,HasInput,statusType,Task");
			Status makeStatus = (Status)pop.UniqueQuery("Status","statusName,HasInput,statusType,Make");
			task.setStatus(status);
			task.getMake().setStatus(makeStatus);
		}
		pop.saveObject(task);
		pop.saveObject(task.getMake());
		MakeBean makeBean = new MakeBean(task.getMake());
		makeBean.changeElementShotStatus();
	}
	public void updateExpectHours(int hours)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		this.task.setExceptHours(hours);
		pop.saveObject(task);
	}
	

	public void setIsDoing() {
		// TODO Auto-generated method stub
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Iterator ite_doingtask = pop.Query("Task", "reguser.regUserId,"+task.getReguser().getRegUserId()+",status.statusName,IsDoing", null,null).iterator();
		while(ite_doingtask.hasNext())
		{
			Task task = (Task)ite_doingtask.next();
			Status status = (Status)pop.UniqueQuery("Status","statusName,StandBy,statusType,Task");
			Status makeStatus = (Status)pop.UniqueQuery("Status","statusName,StandBy,statusType,Make");
			task.setStatus(status);
			pop.saveObject(task);
			
			task.getMake().setStatus(makeStatus);
			pop.saveObject(task.getMake());			
		}
		Status status = (Status)pop.UniqueQuery("Status","statusName,IsDoing,statusType,Task");
		Status makeStatus = (Status)pop.UniqueQuery("Status","statusName,IsDoing,statusType,Make");
		task.setStatus(status);
		if (task.getExactStartTime()==null)	task.setExactStartTime(new Date()); //if has started ,not setStartTime
		pop.saveObject(task);
		this.task.getMake().setStatus(makeStatus);
		pop.saveObject(task.getMake());
		
		
	}
	public void setWaitingConfirm() {
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Status status = (Status)pop.UniqueQuery("Status","statusName,WaitingConfirm,statusType,Task");
		task.setStatus(status);
		pop.saveObject(task);
		
		Status makeStatus = (Status)pop.UniqueQuery("Status","statusName,WaitingConfirm,statusType,Make");
		this.task.getMake().setStatus(makeStatus);
		pop.saveObject(task.getMake());
		
	}
	public void setFinish() {
		// TODO Auto-generated method stub
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Status status = (Status)pop.UniqueQuery("Status","statusName,Finished,statusType,Task");
		task.setStatus(status);
		task.setExactEndTime(new Date());
		pop.saveObject(task);
		Iterator ite_Tasks = task.getMake().getTasks().iterator();
		while(ite_Tasks.hasNext())
		{
			Task task = (Task) ite_Tasks.next();
			if (task.getStatus().getStatusName().equals("Finished")==false)
			{
				return;
			}
		}
		Status makeStatus = (Status)pop.UniqueQuery("Status","statusName,Finished,statusType,Make");
		task.getMake().setStatus(makeStatus);
		pop.saveObject(task.getMake());
		MakeBean makeBean = new MakeBean(task.getMake());
		makeBean.changeElementShotStatus();
		return;
	}
	public void setNotPass()
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Status status = (Status)pop.UniqueQuery("Status","statusName,ConfirmNotPassed,statusType,Task");
		task.setStatus(status);
		pop.saveObject(task);
		
		Status makeStatus = (Status)pop.UniqueQuery("Status","statusName,ConfirmNotPassed,statusType,Make");
		this.task.getMake().setStatus(makeStatus);
		pop.saveObject(task.getMake());
		
	}
	public void setPass()
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Status status = (Status)pop.UniqueQuery("Status","statusName,ConfirmPassed,statusType,Task");
		task.setStatus(status);
		pop.saveObject(task);
		
		Status makeStatus = (Status)pop.UniqueQuery("Status","statusName,ConfirmPassed,statusType,Make");
		this.task.getMake().setStatus(makeStatus);
		pop.saveObject(task.getMake());
		
	}
	public Integer getTaskId() {
		
		return this.task.getTaskId();
	}
	public String getTaskDescription() {
		
		return this.task.getTaskDescription();
	}
	public String getStatusName() {
	
		return this.task.getStatus().getStatusName();
		
	}
	public Task getTask() {
		// TODO Auto-generated method stub
		return this.task;
		
	}
	public Integer getStatusId() {
		
		return this.task.getStatus().getStatusId();
	}
	public String getConfirmerEmployeeNamesByProjectId(int projectId){
		StringBuffer sb=new StringBuffer();
		Set confirmSet=task.getMake().getCategory().getConfirms();
		for(Iterator iter=confirmSet.iterator();iter.hasNext();){
			Confirm confirm=(Confirm)iter.next();
			if(confirm.getProject().getProjectId().equals(projectId)){
				sb.append(confirm.getReguser().getEmployee().getEmpName());
				sb.append(",");
			}
		}
		return sb.toString();
	}
	public void setIsUsable(boolean flag){
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		task.getMake().setIsUsable(flag);
		pop.saveObject(task);
	}
	public boolean canStart() {
		if ( this.getUnFinishedMakes().size()==0 && task.getStatus().getStatusId()!=50 )
		{
			return true;			
		}
		else return false;
	}
}
